﻿Imports Controle
Imports UtilPadrao

Public Class frmPontoAtendimento

#Region "Variáveis"
    Private iCodLogradouro As Int32
    Private iCodTipoPA As Int32
    Private iCodEstado As Int32

    Private objPA As New ctrPA
    Private objTIpoPA As New ctrTipoPA
#End Region

#Region "Propriedades"

    Private Property CodEstado As Int32
        Get
            Return iCodEstado
        End Get
        Set(ByVal value As Int32)
            iCodEstado = value
            carregaCboCidade(iCodEstado)
        End Set
    End Property

#End Region

#Region "Métodos"

    Private Sub carregarCboEstado()
        Dim dtbEstados As New DataTable
        Dim objUF As New ctrUF

        Try

            dtbEstados = objUF.selecionaDadosEstado(0)

            With cboEstado
                .DataSource = dtbEstados
                .DisplayMember = "ds_uf_sigla"
                .ValueMember = "cd_uf"
            End With

        Catch ex As Exception
            MessageBox.Show(ex.Message, Application.ProductName, MessageBoxButtons.OK, MessageBoxIcon.Error)
        End Try

    End Sub

    Private Sub carregaCboCidade(ByVal iCodEstado As Int32)
        Dim dtbCidade As New DataTable
        Dim objCidade As New ctrCidade

        Try
            Me.cboCidade.DataSource = Nothing

            If Me.CodEstado > 0 Then

                dtbCidade = objCidade.selecionaCidadePorEstado(Me.CodEstado)

                With cboCidade
                    .Items.Clear()
                    .DataSource = dtbCidade
                    .DisplayMember = "ds_cidade_nome"
                    .ValueMember = "cd_cidade"
                End With
            End If
            
        Catch ex As Exception
            MessageBox.Show(ex.Message, Application.ProductName, MessageBoxButtons.OK, MessageBoxIcon.Error)
        End Try

    End Sub

    Private Sub preencherEndereco(ByVal sCep As String, ByVal sLogradouro As String)
        Dim objCep As New ctrCep
        Dim objLogradouro As ctrLogradouro
        Dim drEndereco As DataRow = Nothing
        Dim dtbEnderecos As DataTable
        Dim frmDialogo As frmPesquisa

        Try

            limpaCampos(grpEndereco)

            If Not String.IsNullOrEmpty(sCep.Replace("-", "")) Then
                drEndereco = objCep.selecionarEnderecoPorCep(sCep)
            Else
                objLogradouro = New ctrLogradouro
                dtbEnderecos = objLogradouro.selecionarEnderecoPorLogradouro(sLogradouro)
                If dtbEnderecos.Rows.Count > 1 Then
                    frmDialogo = New frmPesquisa

                    With frmDialogo
                        .Titulo = "Pesquisar Endereços"
                        .CarregaTela(dtbEnderecos)
                        .CampoSelecao = 7
                        If .DT.Rows.Count > 0 Then
                            .ShowDialog()
                            If (.DialogResult = Windows.Forms.DialogResult.OK) Then
                                sCep = .ID
                                drEndereco = objCep.selecionarEnderecoPorCep(sCep)
                            End If
                        Else
                            MsgBox("Registro não encontrado", MsgBoxStyle.Exclamation, Me.Text)
                            Exit Sub
                        End If
                    End With
                Else
                    drEndereco = dtbEnderecos.Rows(0)
                End If
            End If

            If Not IsNothing(drEndereco) Then
                With drEndereco
                    Me.txtCep.Text = Formatacao.formatCep(conversao.nuloParaVazio(.Item("cep")))
                    Me.cboEstado.SelectedValue = conversao.nuloParaZero(.Item("coduf"))
                    Me.cboCidade.SelectedValue = conversao.nuloParaZero(.Item("codcidade"))
                    Me.iCodLogradouro = conversao.nuloParaZero(.Item("codLogradouro"))
                    Me.txtLogradouro.Text = conversao.nuloParaVazio(.Item("logradouro"))
                    Me.txtBairro.Text = conversao.nuloParaVazio(.Item("bairro"))

                    Me.txtNumero.Focus()
                End With
            Else
                MessageBox.Show("Cep não localizado.", "Atenção", MessageBoxButtons.OK)
                Me.txtCep.Focus()
            End If


        Catch ex As Exception
            MessageBox.Show(ex.Message, Application.ProductName, MessageBoxButtons.OK, MessageBoxIcon.Error)
        End Try

    End Sub

#End Region

#Region "Eventos"

    Private Sub frmPA_alteraModo(ByVal eModoAtual As FronteiraPadrao.frmPadrao.eModo) Handles Me.alteraModo

        Me.fraGeral.Enabled = Not (eModoAtual = eModo.eAguardando)
        Me.txtCodigo.Enabled = (eModoAtual = eModo.eAguardando)
        Me.btnSelecionarPontoAtendimento.Enabled = (eModoAtual = eModo.eAguardando)
        Me.lblCodigo.Enabled = (eModoAtual = eModo.eAguardando)

    End Sub

    Private Sub frmPA_carregaDados(ByVal iChave As Integer) Handles Me.carregaDados
        Dim drPA As DataRow
        Try

            If conversao.nuloParaZero(MyBase.chave) <> 0 Then
                drPA = Me.objPA.selecionarDadosPA(Me.chave)

                If Not IsNothing(drPA) Then
                    With drPA
                        Me.txtCodigo.Text = conversao.nuloParaVazio(.Item("codPA"))
                        Me.txtDescricao.Text = conversao.nuloParaVazio(.Item("descPA"))
                        Me.iCodTipoPA = conversao.nuloParaZero(.Item("codTipoPA"))
                        Me.txtDescTipoPA.Text = conversao.nuloParaVazio(.Item("descTipoPA"))
                        Me.txtCep.Text = UtilPadrao.Formatacao.formatCep(conversao.nuloParaVazio(.Item("cep")))
                        Me.cboEstado.SelectedValue = conversao.nuloParaZero(.Item("coduf"))
                        Me.cboCidade.SelectedValue = conversao.nuloParaZero(.Item("codcidade"))
                        Me.iCodLogradouro = conversao.nuloParaZero(.Item("codLogradouro"))
                        Me.txtLogradouro.Text = conversao.nuloParaVazio(.Item("logradouro"))
                        Me.txtBairro.Text = conversao.nuloParaVazio(.Item("bairro"))
                        Me.txtNumero.Text = conversao.nuloParaVazio(.Item("numero"))
                        Me.txtComplemento.Text = conversao.nuloParaVazio(.Item("complemento"))
                    End With
                Else
                    MessageBox.Show("Dados do ponto de atendimento não encontrados.", Me.Text, MessageBoxButtons.OK, MessageBoxIcon.Information)
                End If
            End If

        Catch ex As Exception
            MessageBox.Show(ex.Message, Application.ProductName, MessageBoxButtons.OK, MessageBoxIcon.Error)
        End Try


    End Sub

    Private Sub frmPA_excluir(ByRef bCancel As Boolean) Handles Me.excluir

        Try
            bCancel = True
            If Me.objPA.excluirPA(Me.chave) Then
                bCancel = False
            End If

        Catch ex As Exception

            MessageBox.Show(ex.Message, Application.ProductName, MessageBoxButtons.OK, MessageBoxIcon.Error)
        End Try


    End Sub

    Private Sub frmPA_gravar(ByRef bCancel As Boolean) Handles Me.gravar

        Try
            bCancel = True

            If Me.objPA.gravarPA(Me.chave,
                                 Me.txtDescricao.Text,
                                 Me.iCodTipoPA,
                                 Me.iCodLogradouro,
                                 Me.txtNumero.Text,
                                 Me.txtComplemento.Text) Then
                bCancel = False
                Me.chave = Me.objPA.CodPA

            End If

        Catch ex As Exception
            MessageBox.Show(ex.Message, Application.ProductName, MessageBoxButtons.OK, MessageBoxIcon.Error)
        End Try


    End Sub

    Private Sub frmRepresentantes_limpaCampo() Handles Me.limpaCampo

        Me.limpaCampos(Me)

    End Sub

    Private Sub btnSelecionarTipoPA_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSelecionarTipoPA.Click
        Dim sSql As String
        Dim frmDialogo As New frmPesquisa

        Try
            sSql = Me.objTIpoPA.sqlConsulta()

            With frmDialogo
                .Sql = sSql
                .Titulo = "Pesquisar Tipo de Ponto de Atendimento"
                .CarregaTela(Nothing)
                If .DT.Rows.Count > 0 Then
                    .ShowDialog()
                    If (.DialogResult = Windows.Forms.DialogResult.OK) Then
                        Me.iCodTipoPA = conversao.toInt32(.ID)
                        Me.txtDescTipoPA.Text = conversao.nuloParaVazio(.DescSelecionada)
                    End If
                Else
                    MsgBox("Registro não encontrado", MsgBoxStyle.Exclamation, Me.Text)
                    Exit Sub
                End If
            End With

        Catch ex As Exception
            MsgBox(ex.Message, MsgBoxStyle.Critical, Application.ProductName)
        End Try
    End Sub

    Private Sub frmPontoAtendimento_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        Try

            Me.carregarCboEstado()

        Catch ex As Exception
            MessageBox.Show(ex.Message, Application.ProductName, MessageBoxButtons.OK, MessageBoxIcon.Error)
        End Try
    End Sub

    Private Sub cboEstado_SelectedValueChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles cboEstado.SelectedValueChanged
        Try

            If UtilPadrao.conversao.toInt32(Me.cboEstado.SelectedValue) > 0 Then
                Me.CodEstado = Me.cboEstado.SelectedValue
            End If

        Catch ex As Exception
            MessageBox.Show(ex.Message, Application.ProductName, MessageBoxButtons.OK, MessageBoxIcon.Error)
        End Try

    End Sub

#End Region

    Private Sub txtCep_LostFocus(ByVal sender As Object, ByVal e As System.EventArgs) Handles txtCep.LostFocus
        Try

            If Not String.IsNullOrEmpty(txtCep.Text) Then
                Me.preencherEndereco(Me.txtCep.Text, String.Empty)
            End If

        Catch ex As Exception
            MessageBox.Show(ex.Message, Application.ProductName, MessageBoxButtons.OK, MessageBoxIcon.Error)
        End Try
    End Sub

    Private Sub txtLogradouro_LostFocus(ByVal sender As Object, ByVal e As System.EventArgs) Handles txtLogradouro.LostFocus
        Try

            If Not String.IsNullOrEmpty(Me.txtLogradouro.Text) Then
                Me.preencherEndereco(String.Empty, Me.txtLogradouro.Text)
            End If

        Catch ex As Exception
            MessageBox.Show(ex.Message, Application.ProductName, MessageBoxButtons.OK, MessageBoxIcon.Error)
        End Try
    End Sub

    Private Sub btnSelecionarPontoAtendimento_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnSelecionarPontoAtendimento.Click
        Dim sSql As String
        Dim frmDialogo As New frmPesquisa

        Try
            sSql = Me.objPA.sqlConsulta()

            With frmDialogo
                .Sql = sSql
                .Titulo = "Pesquisar Ponto de Atendimento"
                .CarregaTela(Nothing)
                If .DT.Rows.Count > 0 Then
                    .ShowDialog()
                    If (.DialogResult = Windows.Forms.DialogResult.OK) Then
                        Me.chave = conversao.nuloParaZero(.ID)
                    End If
                Else
                    MsgBox("Registro não encontrado", MsgBoxStyle.Exclamation, Me.Text)
                    Exit Sub
                End If
            End With

        Catch ex As Exception
            MsgBox(ex.Message, MsgBoxStyle.Critical, Application.ProductName)
        End Try
    End Sub

    Private Sub txtCodigo_Leave(ByVal sender As Object, ByVal e As System.EventArgs) Handles txtCodigo.Leave

        Try

            If Not String.IsNullOrEmpty(Me.txtCodigo.Text) Then
                Me.chave = conversao.nuloParaZero(Me.txtCodigo.Text)
            End If

        Catch ex As Exception
            MessageBox.Show(ex.Message, Application.ProductName, MessageBoxButtons.OK, MessageBoxIcon.Error)
        End Try
    End Sub

End Class